package datasource.image;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.StringTokenizer;

public class ImageUtil {

	public static Date stringDate2Long(String time) {
		StringTokenizer st = new StringTokenizer(time, " ,-");
		String month = st.nextToken().toLowerCase().trim();
		String dayStr = st.nextToken();
		char c1 = dayStr.charAt(0);
		dayStr = "" + c1;
		if (dayStr.length() >= 2) {
			char c2 = dayStr.charAt(1);
			if (Character.isDigit(c2)) {
				dayStr += c2;
			}
		}
		int day = Integer.parseInt(dayStr);
		int year = Integer.parseInt(st.nextToken());
		String[] mmm = new String[] { "jan", "feb", "march", "april", "may",
				"jun", "jul", "august", "sep", "oct", "nov", "dec" };
		String[] num = new String[] { "01", "02", "03", "04", "05", "06", "07",
				"08", "09", "10", "11", "12" };
		HashMap<String, String> map = new HashMap<String, String>();
		for (int i = 0; i < mmm.length; i++) {
			map.put(mmm[i], num[i]);
		}
		String month_number = map.get(month);
		DateFormat sdf = new SimpleDateFormat("yyyy:MM:dd");
		Date d;
		try {
			d = sdf.parse("" + year + ":" + month_number + ":" + day);
			return d;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static void parseSocialImage(String root) {
		File rootDir = new File(root);
		String[] concepts = new String[] { "water", "plant", "people", "sky",
				"night", "animal", "sunset", "indoor", "structure" };
		int count = 0;
		for (File user : rootDir.listFiles()) {
			for (File event : user.listFiles()) {
				for (File img : event.listFiles()) {
					String name = img.getAbsolutePath();
					if (name.endsWith(".jpg")) {
						count++;
						name = name.substring(0, name.lastIndexOf("."))
								+ ".txt";
						try {
							BufferedWriter bw = new BufferedWriter(
									new FileWriter(name));
							bw.write("people:\nconcepts:");
							for (String c : concepts) {
								bw.write(c + ",");
							}
							bw.write("\n");
							bw.flush();
							bw.close();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}

					}
				}
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		parseSocialImage("C:\\Dropbox\\courses\\pictures from FB");
		// TODO Auto-generated method stub

	}

}
